package userTests;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.io.File;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.*;
import static org.openqa.selenium.OutputType.*;

public class PersonalInfoUserTests {
    FirefoxDriver wd;
    
    @BeforeClass
    public void setUp() throws Exception {
        wd = new FirefoxDriver();
        wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }
    
    @Test
    public void testPersonalInfoUser() {
        wd.get("http://testwmt.wdev.com.ua/");
        wd.findElement(By.linkText("Sign In")).click();
        wd.findElement(By.id("login")).click();
        wd.findElement(By.id("login")).clear();
        wd.findElement(By.id("login")).sendKeys("+380955413233");
        wd.findElement(By.id("password")).click();
        wd.findElement(By.id("password")).clear();
        wd.findElement(By.id("password")).sendKeys("!23Qwe");
        wd.findElement(By.id("lnk-sign-in-submit")).click();
        wd.findElement(By.id("login-form-submit-button")).click();
        wd.findElement(By.linkText("Мой профиль")).click();
        wd.findElement(By.linkText("Персональные данные")).click();
        wd.findElement(By.id("wmprph_lastname")).click();
        wd.findElement(By.id("wmprph_lastname")).clear();
        wd.findElement(By.id("wmprph_lastname")).sendKeys("Komoleva");
        wd.findElement(By.id("wmprph_firstname")).click();
        wd.findElement(By.id("wmprph_firstname")).clear();
        wd.findElement(By.id("wmprph_firstname")).sendKeys("Chandra");
        wd.findElement(By.id("wmprph_passport_series")).click();
        wd.findElement(By.id("wmprph_passport_series")).clear();
        wd.findElement(By.id("wmprph_passport_series")).sendKeys("EK");
        wd.findElement(By.id("wmprph_passport_number")).click();
        wd.findElement(By.id("wmprph_passport_number")).clear();
        wd.findElement(By.id("wmprph_passport_number")).sendKeys("876345");
        wd.findElement(By.id("wmtmbpr_itn")).click();
        wd.findElement(By.id("wmtmbpr_itn")).clear();
        wd.findElement(By.id("wmtmbpr_itn")).sendKeys("2003445566");
        if (!wd.findElement(By.xpath("//form[@id='Wmt_Member_Profile_Physical_Form_Update']/div[1]/div[2]/fieldset/select//option[2]")).isSelected()) {
            wd.findElement(By.xpath("//form[@id='Wmt_Member_Profile_Physical_Form_Update']/div[1]/div[2]/fieldset/select//option[2]")).click();
        }
        wd.findElement(By.id("profileSubmitBtn")).click();
        wd.findElement(By.id("city")).click();
        wd.findElement(By.id("city")).click();
        wd.findElement(By.id("city")).clear();
        wd.findElement(By.id("city")).sendKeys("S");
        wd.findElement(By.cssSelector("#ui-id-135 > b")).click();
        wd.findElement(By.id("wmaddr_address")).click();
        wd.findElement(By.id("wmaddr_address")).clear();
        wd.findElement(By.id("wmaddr_address")).sendKeys("Line 378");
        wd.findElement(By.id("wmaddr_zip_code")).click();
        wd.findElement(By.id("wmaddr_zip_code")).clear();
        wd.findElement(By.id("wmaddr_zip_code")).sendKeys("123654");
        if (wd.findElement(By.id("wmtmbpr_same_address")).isSelected()) {
            wd.findElement(By.id("wmtmbpr_same_address")).click();
        }
        if (!wd.findElement(By.xpath("//div[@id='secondaryAddressContainer']/fieldset/select//option[3]")).isSelected()) {
            wd.findElement(By.xpath("//div[@id='secondaryAddressContainer']/fieldset/select//option[3]")).click();
        }
        wd.findElement(By.id("secondary_city")).click();
        wd.findElement(By.id("secondary_city")).clear();
        wd.findElement(By.id("secondary_city")).sendKeys("Funnycity");
        wd.findElement(By.id("secondary_wmaddr_address")).click();
        wd.findElement(By.id("secondary_wmaddr_address")).clear();
        wd.findElement(By.id("secondary_wmaddr_address")).sendKeys("Play Str., 34");
        wd.findElement(By.id("secondary_wmaddr_zip_code")).click();
        wd.findElement(By.id("secondary_wmaddr_zip_code")).clear();
        wd.findElement(By.id("secondary_wmaddr_zip_code")).sendKeys("987654");
        wd.findElement(By.id("profileSubmitBtn")).click();
        wd.findElement(By.linkText("Общая информация")).click();
        wd.findElement(By.linkText("Персональные данные")).click();
        wd.findElement(By.id("profileSubmitBtn")).click();
        wd.findElement(By.xpath("//div[@id='tab_personal']/div[1]/ul/li")).click();
        
    }
    
    @AfterClass
    public void tearDown() {
        wd.quit();
    }
    
    public static boolean isAlertPresent(FirefoxDriver wd) {
        try {
            wd.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }
}

